package com.xingxing.Dao;

import com.xingxing.Bean.File;
import com.xingxing.Util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class FileDao {
    public static boolean insert(File file) {
        int num = 0;
        Connection conn = null;
        try {
            conn = C3P0Util.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "insert into File values(?,?,?,?,?,?,?);";
            num = runner.update(conn, sql,new Object[]{
                    file.getFileID(),
                    file.getFileName(),
                    file.getUserAccount(),
                    file.getFileType(),
                    file.getFileSize(),
                    file.getUploadDate(),
                    file.getFileDirectory()
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (num > 0) return true;
        return false;
    }

    public static ArrayList<File> findAll() {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from File";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(File.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static ArrayList<File> findUserFile(String userAccount) {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from File where userAccount=?";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(File.class),new Object[]{userAccount});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static File find(String fileID) {
        File file = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from File where fileID = ?";
            file = (File) runner.query(C3P0Util.getConnection(), sql, new BeanHandler(File.class), new Object[]{fileID});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static boolean update(File file){
        if (delete(file.getFileID()) && insert(file)) return true;
        else return false;
    }

    public static boolean delete(String fileID) {
        try {
            QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
            String sql = "delete from File where fileID = ?";
            if (runner.update(sql, new Object[]{fileID}) > 0) return true;
            return false;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }
}
